{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "UnboundLocalError",
     "evalue": "local variable 'days' referenced before assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnboundLocalError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32mD:\\temp/ipykernel_17204/2752492393.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mm0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     19\u001b[0m     \u001b[0mm\u001b[0m\u001b[1;33m==\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhow_many_days_month\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mm\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     21\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     22\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\temp/ipykernel_17204/2752492393.py\u001b[0m in \u001b[0;36mhow_many_days_month\u001b[1;34m(y, m)\u001b[0m\n\u001b[0;32m     12\u001b[0m        \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m            \u001b[0mdays\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m28\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m    \u001b[1;32mreturn\u001b[0m \u001b[0mdays\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     15\u001b[0m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m2021\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m \u001b[0mm0\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m13\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'days' referenced before assignment"
     ]
    }
   ],
   "source": [
    "#生成并打印2021年的日历，要求以类似如下格式打印：\n",
    "\n",
    "def input_yms():\n",
    "   '''提示用户输入年月日,无需传入参数，返回一个年月日数值为元素的元组'''\n",
    "   inputstr = ('year','month','day')\n",
    "   y = int(input('Please input the number of {[0]}:'.format(inputstr)))\n",
    "   m = int(input('Please input the number of {[1]}:'.format(inputstr)))\n",
    "   d = int(input('Please input the number of {[2]}:'.format(inputstr)))\n",
    "   return y,m,d\n",
    "def how_many_days_month(y,m):\n",
    "   '''计算这一月的天数，按顺序传入年月日'''\n",
    "   # 闰年判定方法：能被400整除。或者能被4整除但不能被100整除。\n",
    "   if m in [1,3,5,7,8,10,12]:\n",
    "       days = 31\n",
    "   elif m in [4,6,9,11]:\n",
    "       days = 30\n",
    "   elif m == 2:\n",
    "       if y//400 == 0 or (y // 4 and y // 100 != 0):\n",
    "           days = 29\n",
    "       else:\n",
    "           days = 28\n",
    "   return days\n",
    "\n",
    "\n",
    "# 计算1，2月时，需要当作上一年的13、14月使用\n",
    "def get_which_week(y,m,d):\n",
    "    \n",
    "   '''计算星期几的函数，按顺序传入年月日'''\n",
    "   if m == 1: # w所在的行为计算公式，但如果是1，2月，则需要将其作为前一年的13、14月来计算\n",
    "       y = y - 1\n",
    "       m = 13\n",
    "   elif m == 2:\n",
    "       y = y - 1\n",
    "       m = 14\n",
    "   w =((d + 2 * m+ 3 * (m + 1)// 5 + y + y // 4 - y // 100 + y // 400) % 7 +1))\n",
    "   return w\n",
    "   y,m,d = input_yms()\n",
    "days = how_many_days_month(y,m)\n",
    "\n",
    "print(f'\\n{y}年{m}月 格里高利历')\n",
    "print('一 二 三 四 五 六 日')\n",
    "print('-'*20)\n",
    "for i in range(1,days+1):\n",
    "   w = get_which_week(y,m,i)\n",
    "   if i == 1:\n",
    "       print(f\"{' '*(w-1)*3}\",end = '')\n",
    "   else:\n",
    "       if w == 1:\n",
    "           print('')\n",
    "   print(f\"{i:2d}\", end = ' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         January\n",
      "Mo  Tu  We  Th  Fr  Sa  Su  \n",
      "\n",
      "                    1   2   \n",
      "\n",
      "3   4   5   6   7   8   9   \n",
      "\n",
      "10  11  12  13  14  15  16  \n",
      "\n",
      "17  18  19  20  21  22  23  \n",
      "\n",
      "24  25  26  27  28  29  30  \n",
      "\n",
      "31  \n",
      "\n",
      "         Feburary\n",
      "Mo  Tu  We  Th  Fr  Sa  Su  \n",
      "\n",
      "    1   2   3   4   5   6   \n",
      "\n",
      "7   8   9   10  11  12  13  \n",
      "\n",
      "14  15  16  17  18  19  20  \n",
      "\n",
      "21  22  23  24  25  26  27  \n",
      "\n",
      "28  \n",
      "\n",
      "         March\n",
      "Mo  Tu  We  Th  Fr  Sa  Su  \n",
      "\n",
      "    1   2   3   4   5   6   \n",
      "\n",
      "7   8   9   10  11  12  13  \n",
      "\n",
      "14  15  16  17  18  19  20  \n",
      "\n",
      "21  22  23  24  25  26  27  \n",
      "\n",
      "28  29  30  31  \n",
      "\n",
      "         April\n",
      "Mo  Tu  We  Th  Fr  Sa  Su  \n",
      "\n",
      "                1   2   3   \n",
      "\n",
      "4   5   6   7   8   9   10  \n",
      "\n",
      "11  12  13  14  15  16  17  \n",
      "\n",
      "18  19  20  21  22  23  24  \n",
      "\n",
      "25  26  27  28  29  30  \n",
      "\n",
      "         May\n",
      "Mo  Tu  We  Th  Fr  Sa  Su  \n",
      "\n",
      "                        1   \n",
      "\n",
      "2   3   4   5   6   7   8   \n",
      "\n",
      "9   10  11  12  13  14  15  \n",
      "\n",
      "16  17  18  19  20  21  22  \n",
      "\n",
      "23  24  25  26  27  28  29  \n",
      "\n",
      "30  31  \n",
      "\n",
      "         June\n",
      "Mo  Tu  We  Th  Fr  Sa  Su  \n",
      "\n",
      "        1   2   3   4   5   \n",
      "\n",
      "6   7   8   9   10  11  12  \n",
      "\n",
      "13  14  15  16  17  18  19  \n",
      "\n",
      "20  21  22  23  24  25  26  \n",
      "\n",
      "27  28  29  30  \n",
      "\n",
      "         July\n",
      "Mo  Tu  We  Th  Fr  Sa  Su  \n",
      "\n",
      "                1   2   3   \n",
      "\n",
      "4   5   6   7   8   9   10  \n",
      "\n",
      "11  12  13  14  15  16  17  \n",
      "\n",
      "18  19  20  21  22  23  24  \n",
      "\n",
      "25  26  27  28  29  30  31  \n",
      "\n",
      "\n",
      "\n",
      "         August\n",
      "Mo  Tu  We  Th  Fr  Sa  Su  \n",
      "\n",
      "1   2   3   4   5   6   7   \n",
      "\n",
      "8   9   10  11  12  13  14  \n",
      "\n",
      "15  16  17  18  19  20  21  \n",
      "\n",
      "22  23  24  25  26  27  28  \n",
      "\n",
      "29  30  31  \n",
      "\n",
      "         September\n",
      "Mo  Tu  We  Th  Fr  Sa  Su  \n",
      "\n",
      "            1   2   3   4   \n",
      "\n",
      "5   6   7   8   9   10  11  \n",
      "\n",
      "12  13  14  15  16  17  18  \n",
      "\n",
      "19  20  21  22  23  24  25  \n",
      "\n",
      "26  27  28  29  30  \n",
      "\n",
      "         October\n",
      "Mo  Tu  We  Th  Fr  Sa  Su  \n",
      "\n",
      "                    1   2   \n",
      "\n",
      "3   4   5   6   7   8   9   \n",
      "\n",
      "10  11  12  13  14  15  16  \n",
      "\n",
      "17  18  19  20  21  22  23  \n",
      "\n",
      "24  25  26  27  28  29  30  \n",
      "\n",
      "31  \n",
      "\n",
      "         November\n",
      "Mo  Tu  We  Th  Fr  Sa  Su  \n",
      "\n",
      "    1   2   3   4   5   6   \n",
      "\n",
      "7   8   9   10  11  12  13  \n",
      "\n",
      "14  15  16  17  18  19  20  \n",
      "\n",
      "21  22  23  24  25  26  27  \n",
      "\n",
      "28  29  30  \n",
      "\n",
      "         December\n",
      "Mo  Tu  We  Th  Fr  Sa  Su  \n",
      "\n",
      "            1   2   3   4   \n",
      "\n",
      "5   6   7   8   9   10  11  \n",
      "\n",
      "12  13  14  15  16  17  18  \n",
      "\n",
      "19  20  21  22  23  24  25  \n",
      "\n",
      "26  27  28  29  30  31  \n",
      "\n"
     ]
    }
   ],
   "source": [
    "week=['Mo','Tu','We','Th','Fr','Sa','Su']\n",
    "month=['January','Feburary','March','April','May','June','July','August','September','October','November','December']\n",
    "\n",
    "\n",
    "def day(x):\n",
    "    if(x==2):\n",
    "        return 28\n",
    "    elif(x==1 or x==3 or x==5 or x==7 or x==8 or x==10 or x==12):\n",
    "        return 31\n",
    "    else:\n",
    "        return 30\n",
    "a=4\n",
    "tab=[5,1,1,4,6,2,4,0,3,5,1,3]\n",
    "for i in range(0,12):\n",
    "    print(' '*9+month[i])\n",
    "    for j in range(7):\n",
    "        print(\"{:3s}\".format(week[j])+' ',end='')\n",
    "    print('\\n')\n",
    "    print(' '*(tab[i]*a),end='')\n",
    "    for x in range (1,day(i+1)+1):\n",
    "        print(\"{:<4d}\".format(x),end='') \n",
    "        if (tab[i]+x)%7==0:\n",
    "            print('\\n')\n",
    "    print('\\n')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.7 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "2dbe5485cf12282196b80a8ac362415f1d4830d1445e2229f35f94c570ef032e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
